package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderTimeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersTimeController {

    @Reference
    private OrderTimeService orderservice;

    //增加
    @RequestMapping("/addOrder")
    public Result add(@RequestBody Order order){
        try {
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    //查询
    @RequestMapping("/selectOrder")
    public Result selectCheckItem(@RequestBody QueryPageBean queryPageBean){
        try {
            System.out.println(queryPageBean);
            PageResult pageResult = orderservice.selectOrder(queryPageBean);
            System.out.println(pageResult);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    @RequestMapping("/deleteOrder")
    public Result deleteCheckItem(Integer id){
        try {
            int i = orderservice.deleteOrder(id);
            boolean bool = i>=1 ? true:false;
            if (bool){
                return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
            }
            return new Result(true,MessageConstant.DELETE_ODRDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/selectOneOrder")
    public Result selectOneOrder(Integer id){
        try {
            Order order = orderservice.selectOneOrder(id);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,order);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/selectSetmeal")
    public Result selectStream(){
        try {
            List<Setmeal> setmeals = orderservice.selectStream();
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,setmeals);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    //更新
    @RequestMapping("/updateOrder")
    public Result updateCheckItem(Integer id,@RequestBody Order order){
        try {
            int i = orderservice.updateOrder(id, order);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
}
